package com.ourteam.product.api;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ourteam.frame.controller.AbstractCRUDServiceController;
import com.ourteam.product.ui.dao.IProductUIActionDAO;
import com.ourteam.product.ui.dao.IProductUIActionGroupRelateDAO;
import com.ourteam.product.ui.dao.ProductUIActionDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUIActionGroupDAOQueryBean;
import com.ourteam.product.ui.dao.ProductUIActionGroupRelateDAOQueryBean;
import com.ourteam.product.ui.domain.ProductUIActionBean;
import com.ourteam.product.ui.domain.ProductUIActionGroupBean;
import com.ourteam.product.ui.domain.ProductUIActionGroupRelateBean;
import com.ourteam.product.ui.service.IProductUIService;
import com.ourteam.product.ui.service.ProductUIServiceFactory;

import net.ui.model.action.ActionBean;
import net.ui.model.action.ActionGroupBean;

@Controller
@CrossOrigin
@RequestMapping("/api/PRODUCT/UI/productUIActionGroup")
public class ProductUIActionGroupAPI extends
		AbstractCRUDServiceController<ProductUIActionGroupDAOQueryBean, ProductUIActionGroupSearchModel, ProductUIActionGroupBean> {

	private static final IProductUIService PRODUCT_UI_SERVICE = ProductUIServiceFactory.getProductUIService();

	@Override
	protected ProductUIActionGroupDAOQueryBean newQueryBean() {
		return new ProductUIActionGroupDAOQueryBean();
	}

	@Override
	protected int queryCount(ProductUIActionGroupDAOQueryBean queryBean) throws Exception {
		return PRODUCT_UI_SERVICE.queryProductUIActionGroupCount(queryBean);
	}

	@Override
	protected ProductUIActionGroupBean[] queryData(ProductUIActionGroupDAOQueryBean queryBean) throws Exception {
		return PRODUCT_UI_SERVICE.queryProductUIActionGroups(queryBean);
	}

	@Override
	protected void deleteData(long[] ids) throws Exception {
		PRODUCT_UI_SERVICE.deleteProductUIActionGroups(ids);
	}

	@Override
	protected ProductUIActionGroupBean getDataById(long id) throws Exception {
		return PRODUCT_UI_SERVICE.getProductUIActionGroupById(id);
	}

	@Override
	protected void saveData(ProductUIActionGroupBean dataBean) throws Exception {
		dataBean.setLastModifyTime(new Date());
		dataBean.setLastModifyUser(getLoginUserName());
		if (dataBean.getId() == 0) {
			PRODUCT_UI_SERVICE.addNewProductUIActionGroup(dataBean);
		} else {
			PRODUCT_UI_SERVICE.modifyProductUIActionGroup(dataBean);
		}

	}

	@RequestMapping("/getActionGroup")
	@ResponseBody
	public ActionGroupBean getActionGroup(@RequestParam long id) throws Exception {

		ProductUIActionGroupBean productUIActionGroupBean = PRODUCT_UI_SERVICE.getProductUIActionGroupById(id);

		ActionGroupBean actionGroupBean = new ActionGroupBean();

		actionGroupBean.setId(String.valueOf(productUIActionGroupBean.getId()));

		actionGroupBean.setDesc(productUIActionGroupBean.getRemarks());

		ProductUIActionGroupRelateDAOQueryBean actionGroupRelateQueryBean = new ProductUIActionGroupRelateDAOQueryBean();

		actionGroupRelateQueryBean.setSortProperty(IProductUIActionGroupRelateDAO.SortIndex);

		actionGroupRelateQueryBean.createCriteria().andActionGroupIdEqualTo(productUIActionGroupBean.getId());

		ProductUIActionGroupRelateBean[] actionGroupRelateBeans = PRODUCT_UI_SERVICE
				.queryProductUIActionGroupRelates(actionGroupRelateQueryBean);

		ProductUIActionDAOQueryBean queryBean = new ProductUIActionDAOQueryBean();

		for (int j = 0; j < actionGroupRelateBeans.length; j++) {
			ProductUIActionGroupRelateBean productUIActionGroupRelateBean = actionGroupRelateBeans[j];

			ProductUIActionBean uiActionBean = PRODUCT_UI_SERVICE
					.getProductUIActionById(productUIActionGroupRelateBean.getRelatedActionId());

			ActionBean actionBean = new ActionBean();

			actionBean.setRef(String.valueOf(productUIActionGroupRelateBean.getRelatedActionId()));

			actionBean.setType(productUIActionGroupRelateBean.getActionType());

			if (StringUtils.isNotEmpty(productUIActionGroupRelateBean.getConfirmInfo())) {
				actionBean.setConfirmInfoKey(productUIActionGroupRelateBean.getConfirmInfo());
			} else {
				actionBean.setConfirmInfoKey(uiActionBean.getConfirmInfo());
			}

			if (StringUtils.isNotEmpty(productUIActionGroupRelateBean.getHandler())) {
				actionBean.setHandler(productUIActionGroupRelateBean.getHandler());
			} else {
				actionBean.setHandler(uiActionBean.getHandler());
			}

			if (StringUtils.isNotEmpty(productUIActionGroupRelateBean.getIcon())) {
				actionBean.setIcon(productUIActionGroupRelateBean.getIcon());
			} else {
				actionBean.setIcon(uiActionBean.getIcon());
			}

			if (StringUtils.isNotEmpty(productUIActionGroupRelateBean.getLabelText())) {
				actionBean.setLabel(productUIActionGroupRelateBean.getLabelText());
			} else {
				actionBean.setLabel(uiActionBean.getLabelText());
			}

			if (StringUtils.isNotEmpty(productUIActionGroupRelateBean.getLabelKey())) {
				actionBean.setLabelKey(productUIActionGroupRelateBean.getLabelKey());
			} else {
				actionBean.setLabelKey(uiActionBean.getLabelKey());
			}

			if (StringUtils.isNotEmpty(productUIActionGroupRelateBean.getRemarks())) {
				actionBean.setDesc(productUIActionGroupRelateBean.getRemarks());
			} else {
				actionBean.setDesc(uiActionBean.getRemarks());
			}

			if (StringUtils.isNotEmpty(productUIActionGroupRelateBean.getConfirmInfo())) {
				actionBean.setNeedComfirm(productUIActionGroupRelateBean.getConfirmInfo());
			} else {
				actionBean.setNeedComfirm(uiActionBean.getConfirmInfo());
			}

			queryBean.clear();
			queryBean.setSortProperty(IProductUIActionDAO.SortIndex);
			queryBean.createCriteria().andParentIdEqualTo(uiActionBean.getId());

			ProductUIActionBean[] uiSubActionBean = PRODUCT_UI_SERVICE.queryProductUIActions(queryBean);

			for (int i = 0; i < uiSubActionBean.length; i++) {

				ActionBean subActionBean = new ActionBean();

				subActionBean.setRef(String.valueOf(uiSubActionBean[i].getId()));

				subActionBean.setType(uiSubActionBean[i].getType());

				subActionBean.setConfirmInfoKey(uiSubActionBean[i].getConfirmInfo());

				subActionBean.setHandler(uiSubActionBean[i].getHandler());

				subActionBean.setIcon(uiSubActionBean[i].getIcon());

				subActionBean.setLabel(uiSubActionBean[i].getLabelText());

				subActionBean.setLabelKey(uiSubActionBean[i].getLabelKey());

				subActionBean.setDesc(uiSubActionBean[i].getRemarks());

				actionBean.addSubActionBean(subActionBean);
			}

			actionGroupBean.addActionBean(actionBean);

		}

		return actionGroupBean;
	}

}
